//
//  MainNewsViewController.m
//  WWXELuShang
//
//  Created by lanou on 15/12/18.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "MainNewsViewController.h"
#import "NewsTableViewController.h"
#import "ImageTableViewController.h"
#import "VideoCollectionViewController.h"


@interface MainNewsViewController ()

@end
@interface MainNewsViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong)NewsTableViewController *newsTVC;
@property (nonatomic, strong)ImageTableViewController *imageTVC;

@property (nonatomic, strong)VideoCollectionViewController *videoCVC;
@property (nonatomic, strong)UIScrollView *scroolView;
@property (nonatomic, strong)UISegmentedControl *segmentedControl;



@end

@implementation MainNewsViewController



- (UIScrollView *)scroolView {
    if (!_scroolView) {
        _scroolView = [[UIScrollView alloc] initWithFrame: CGRectMake(0, kHeight / 21, kWidth, self.view.frame.size.height - kHeight / 21)];
        _scroolView.pagingEnabled = YES;
        _scroolView.contentSize = CGSizeMake(kWidth * 3, 0);
        _scroolView.showsVerticalScrollIndicator = NO;
        _scroolView.delegate = self;
        
    }
    
    return _scroolView;
}

- (NewsTableViewController *)newsTVC {
    if (!_newsTVC) {
        _newsTVC = [[NewsTableViewController alloc] initWithStyle:(UITableViewStylePlain)];
        _newsTVC.tableView.frame = CGRectMake(0, 0, kWidth, self.scroolView.frame.size.height - kHeight / 667 * 64);
    }
    return _newsTVC;
}

- (ImageTableViewController *)imageTVC {
    if (!_imageTVC) {
        _imageTVC = [[ImageTableViewController alloc]initWithStyle:(UITableViewStylePlain)];
        _imageTVC.tableView.frame = CGRectMake(kWidth, 0, kWidth, self.scroolView.frame.size.height - kHeight / 667 * 64);
    }
    
    return _imageTVC;
}


- (VideoCollectionViewController *)videoCVC {
    if (!_videoCVC) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        _videoCVC = [[VideoCollectionViewController alloc] initWithCollectionViewLayout:flowLayout];
        _videoCVC.collectionView.frame = CGRectMake(kWidth * 2, 0, kWidth, self.scroolView.frame.size.height - kHeight / 667 * 64);
    }
    
    return _videoCVC;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"smallArrow"] style:(UIBarButtonItemStylePlain) target:self action:@selector(getBack)];
    self.navigationItem.title = @"行车资讯";
    self.segmentedControl = [[UISegmentedControl alloc]initWithItems:@[@"最新车讯", @"汽车美图", @"视频资讯"]];
    self.segmentedControl.frame = CGRectMake(0, 0, kWidth, kHeight / 21);
    [self.view addSubview:self.segmentedControl];
    UIColor *color = kBarColor;
     self.segmentedControl.tintColor = color;
    self.segmentedControl.selectedSegmentIndex = 0;
    [self.segmentedControl addTarget:self action:@selector(changeView:) forControlEvents:(UIControlEventValueChanged)];
    self.view.backgroundColor = kBackGroundColor;
    
    [self.view addSubview:self.scroolView];
    
    [self.scroolView addSubview:self.newsTVC.tableView];
    [self.scroolView addSubview:self.imageTVC.tableView];
    [self.scroolView addSubview:self.videoCVC.collectionView];
    [self addChildViewController:self.newsTVC];
    [self addChildViewController:self.imageTVC];
    [self addChildViewController:self.videoCVC];
    
    // Do any additional setup after loading the view.
}

- (void)changeView:(UISegmentedControl *)segmentedControl {
    
    NSInteger index = segmentedControl.selectedSegmentIndex;
    
    switch (index) {
        case 0:
            
            [self.scroolView setContentOffset:CGPointMake(0, 0) animated:YES];
            
            break;
        
        case 1:
            
            [self.scroolView setContentOffset:CGPointMake(kWidth, 0) animated:YES];
            
            
            break;
        
        case 2:
            [self.scroolView setContentOffset:CGPointMake(kWidth * 2, 0) animated:YES];
            
            break;
            
        default:
            break;
    }
    
    
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    CGFloat currentX = scrollView.contentOffset.x;
     self.segmentedControl.selectedSegmentIndex = currentX / kWidth;
}


- (void)getBack {
    
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
